<?php
declare (strict_types=1);

namespace app\mall\model;

use app\mall\model\traits\CateTrait;
use app\mall\model\traits\DateTrait;
use app\mall\model\traits\FreightTrait;
use app\mall\model\traits\GoodsDetailTrait;
use app\mall\model\traits\GoodsStockTrait;
use app\mall\model\traits\GoodsStockTypeTrait;
use app\mall\model\traits\StatusTrait;
use think\Model;

/**
 * @mixin Model
 * @property integer cate_id
 * @property mixed id
 */
class MallGoods extends MallBase
{
    use GoodsDetailTrait;
    use GoodsStockTrait;
    use GoodsStockTypeTrait;
    use CateTrait;
    use FreightTrait;
    use StatusTrait;
    use DateTrait;

    protected $json = ['cate_ids'];

    /**
     * @param MallBase $mod
     * @return mixed|void
     */
    public static function onBeforeWrite($mod)
    {
        static::cateOnBeforeWrite($mod);
    }

    public function setCateIdAttr($value, $data)
    {
        if ($data['cate_ids']) {
            $this->set('cate_id', $data['cate_ids'][count($data['cate_ids']) - 1]);
        }
    }

    public function searchSkAttr($query, $value, $data)
    {
        if ($value) {
            $query->where('title', 'like', "%{$value}%");
        }
    }

}
